package com.caiyi.financial.nirvana.sms.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {

    public static Logger logger = LoggerFactory.getLogger(DateUtil.class);

    public static final String FORMAT1 = "yyyy-MM-dd";

    /**
     * 计算时间相差天数
     *
     * @param startTime
     * @param endTime
     * @return
     */
    public static int getDateDiffer(Date startTime, Date endTime) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(FORMAT1);
            startTime = sdf.parse(sdf.format(startTime));
            endTime = sdf.parse(sdf.format(endTime));
            Calendar cal = Calendar.getInstance();
            cal.setTime(startTime);
            long time1 = cal.getTimeInMillis();
            cal.setTime(endTime);
            long time2 = cal.getTimeInMillis();
            long between_days = (time2 - time1) / (1000 * 3600 * 24);
            return Integer.parseInt(String.valueOf(between_days));
        } catch (Exception e) {
            logger.error("时间计算转换错误", e);
            throw new RuntimeException(e);
        }
    }
}
